<template>
  <a-table
    :ref="tableRef"
    :columns="columns"
    :data="dataList"
    :stripe="true"
    :pagination="{
      showTotal: true,
      pageSize: searchParams.pageSize,
      current: searchParams.current,
      total,
    }"
    @page-change="onPageChange"
  >
    <template #judgeInfo="{ record }">
      <div>
        <p>Message: {{ record.judgeInfo?.message || "无" }}</p>
        <p>Memory: {{ record.judgeInfo?.memory || "无" }}</p>
        <p>Time: {{ record.judgeInfo?.time || "无" }}</p>
      </div>
    </template>
    <template #createTime="{ record }">
      {{ moment(record.createTime).format("YYYY-MM-DD") }}
    </template>
    <template #updateTime="{ record }">
      {{ moment(record.updateTime).format("YYYY-MM-DD") }}
    </template>
    <template #optional="{ record }">
      <a-space>
        <a-button type="primary" @click="doQuestionPage(record)">
          开始
        </a-button>
      </a-space>
    </template>
  </a-table>
</template>

<script setup lang="ts">
import moment from "moment";
import { onMounted, ref, watchEffect } from "vue";
import {
  Question,
  QuestionSubmitControllerService,
  QuestionSubmitQueryRequest,
} from "../../../generated";
import { useRouter } from "vue-router";
import message from "@arco-design/web-vue/es/message";

const tableRef = ref();

const columns = [
  {
    title: "题号",
    dataIndex: "id",
  },
  {
    title: "编程语言",
    dataIndex: "language",
  },
  {
    title: "用户代码",
    dataIndex: "code",
  },
  {
    title: "判题信息",
    slotName: "judgeInfo",
  },
  {
    title: "判题状态",
    dataIndex: "status",
  },
  {
    title: "题目 id",
    dataIndex: "questionId",
  },
  {
    title: "创建时间",
    slotName: "createTime",
  },
  {
    slotName: "optional",
  },
];

const dataList = ref([]);

const searchParams = ref<QuestionSubmitQueryRequest>({
  title: "",
  tags: [],
  pageSize: 5,
  current: 1,
});

const total = ref(0);

const router = useRouter();

const onPageChange = (page: number) => {
  searchParams.value = {
    ...searchParams.value,
    current: page,
  };
};

const doQuestionPage = (question: Question) => {
  router.push({
    path: `/view/question/${question.id}`,
  });
};

const loadData = async () => {
  const res =
    await QuestionSubmitControllerService.listQuestionSubmitByPageUsingPost(
      searchParams.value
    );
  if (res.code === 0) {
    dataList.value = res.data.records;
    total.value = res.data.total;
  } else {
    message.error("加载失败，" + res.message);
  }
};

watchEffect(() => {
  loadData();
});
onMounted(() => {
  loadData();
});
</script>

<style scoped></style>
